Python for Bioinformatics

This Jupyter notebook is intented to be used alongside the book Python for Bioinformatics

Chapter 8: Introduction to Object Orienting Programming (OOP)


In [3]:
class Square:
    def __init__(self):
        self.side = 1

In [6]:
Bob = Square() # Bob is an instance of Square.
Bob.side #Let’s see the value of side


Out[6]:
1

In [7]:
Bob.side = 5 #Assing a new value to side
Bob.side #Let’s see the new value of side


Out[7]:
5

In [8]:
Krusty = Square()
Krusty.side


Out[8]:
1

Text


In [14]:
class Square:
    def __init__(self):
        self.side=1
        
Bob = Square() # Bob is an instance of Square.
Bob.side #Let's see the value of side


Out[14]:
1

In [15]:
Bob.side = 5 #Assing a new value to side
Bob.side #Let's see the new value of side


Out[15]:
5

In [16]:
Krusty = Square()
Krusty.side


Out[16]:
1

In [8]:
Square.side


Out[8]:
1

In [9]:
Crab = Square()
Crab.side


Out[9]:
1

In [10]:
class Square:
    count = 0
    def __init__(self):
        Square.count += 1
        print("Object created successfully")

In [11]:
Bob = Square()


Object created successfully

In [12]:
Patrick = Square()


Object created successfully

In [13]:
Square.count


Out[13]:
2

In [17]:
class Sequence:
    transcription_table = {'A':'U', 'T':'A', 'C':'G' , 'G':'C'}
    def __init__(self, seqstring):
        self.seqstring = seqstring.upper()
    def transcription(self):
        tt = ""
        for letter in self.seqstring:
            if letter in 'ATCG':
                tt += self.transcription_table[letter]
        return tt

dangerous_virus = Sequence('atggagagccttgttcttggtgtcaa')
dangerous_virus.seqstring


Out[17]:
'ATGGAGAGCCTTGTTCTTGGTGTCAA'

In [18]:
harmless_virus = Sequence('aatgctactactattagtagaattgatgcca')
harmless_virus.seqstring


Out[18]:
'AATGCTACTACTATTAGTAGAATTGATGCCA'

In [20]:
dangerous_virus.transcription()


Out[20]:
'UACCUCUCGGAACAAGAACCACAGUU'

Listing 8.1: seqclass.py: Sequence class


In [21]:
class Sequence:
    transcription_table = {'A':'U', 'T':'A', 'C':'G' , 'G':'C'}
    enz_dict = {'EcoRI':'GAATTC', 'EcoRV':'GATATC'}
    def __init__(self, seqstring):
        self.seqstring = seqstring.upper()
    def restriction(self, enz):
        try:
            enz_target = Sequence.enz_dict[enz]
            return self.seqstring.count(enz_target)
        except KeyError:
            return 0
    def transcription(self):
        tt = ""
        for letter in self.seqstring:
            if letter in 'ATCG':
                tt += self.transcription_table[letter]
        return tt

other_virus = Sequence('atgatatcggagaggatatcggtgtcaa')
other_virus.restriction('EcoRV')


Out[21]:
2

Listing 8.2: orca.py: Orca class


In [1]:
class Mammal():
    """Docstring with class description"""
    # Properties here
    # Methods here

class Orca(Mammal):
    """Docstring with class description"""
    # Properties here
    # Methods here

Listing 8.3: plasmid.py: Plasmid class


In [23]:
class Plasmid(Sequence):
    ab_res_dict = {'Tet':'ctagcat', 'Amp':'CACTACTG'}
    def __init__(self, seqstring):
        Sequence.__init__(self, seqstring)
    def ab_res(self, ab):
        if self.ab_res_dict[ab] in self.seqstring:
            return True
        return False

In [1]:
!conda install biopython -y


Fetching package metadata .........
Solving package specifications: .

Package plan for installation in environment /home/nbcommon/anaconda3_410:

The following NEW packages will be INSTALLED:

    biopython: 1.68-np111py35_0

biopython-1.68 100% |################################| Time: 0:00:00  12.39 MB/s

In [2]:
from Bio.Alphabet import IUPAC
from Bio.Seq import Seq
first_seq = Seq('GCTATGCAGC', IUPAC.unambiguous_dna)
first_seq


Out[2]:
Seq('GCTATGCAGC', IUPACUnambiguousDNA())

In [3]:
first_seq.complement()


Out[3]:
Seq('CGATACGTCG', IUPACUnambiguousDNA())

In [4]:
first_seq.tostring()


/home/nbuser/anaconda3_410/lib/python3.5/site-packages/Bio/Seq.py:341: BiopythonDeprecationWarning: This method is obsolete; please use str(my_seq) instead of my_seq.tostring().
  BiopythonDeprecationWarning)
Out[4]:
'GCTATGCAGC'

In [5]:
first_seq[:10] # slice a sequence


Out[5]:
Seq('GCTATGCAGC', IUPACUnambiguousDNA())

In [6]:
len(first_seq) # get the length of the sequence


Out[6]:
10

In [7]:
first_seq[0] # get one character


Out[7]:
'G'

In [8]:
first_seq


Out[8]:
Seq('GCTATGCAGC', IUPACUnambiguousDNA())

In [9]:
AnotherSeq=first_seq.tomutable()
AnotherSeq.extend("TTTTTTT")
print(AnotherSeq)


GCTATGCAGCTTTTTTT

In [10]:
AnotherSeq.pop()


Out[10]:
'T'

In [11]:
AnotherSeq.pop()


Out[11]:
'T'

In [12]:
print(AnotherSeq)


GCTATGCAGCTTTTT

Listing 8.4: seqclass2.py: Sequence class


In [37]:
class Sequence:
    transcription_table = {'A':'U', 'T':'A', 'C':'G' , 'G':'C'}
    enz_dict = {'EcoRI':'GAATTC', 'EcoRV':'GATATC'}
    def __init__(self, seqstring):
        self.seqstring = seqstring.upper()
    def __len__(self):
        return len(self.seqstring)
    def restriction(self, enz):
        try:
            enz_target = Sequence.enz_dict[enz]
            return self.seqstring.count(enz_target)
        except KeyError:
            return 0
    def transcription(self):
        tt = ""
        for letter in self.seqstring:
            if letter in 'ATCG':
                tt += self.transcription_table[letter]
        return tt
    
M13 = Sequence("ACGACTCTCGACGGCATCCACCCTCTCTGAGA")
len(M13)


Out[37]:
32

In [46]:
class Straight:
    def __init__(self, data):
        self.data = data
        self.index = 0
    def __iter__(self):
        return self
    def __next__(self):
        if self.index == len(self.data):
            raise StopIteration
        answer = self.data[self.index]
        self.index += 1
        return answer

class Reverse:
    def __init__(self, data):
        self.data = data
        self.index = len(data)
    def __iter__(self):
        return self
    def __next__(self):
        if self.index == 0:
            raise StopIteration
        self.index -= 1
        return self.data[self.index]
    
a = Straight("123")
for i in a:
    print(i)


1
2
3

In [45]:
b = Reverse("123")
for i in b:
    print(i)


3
2
1

Listing 8.6: seqwitsm.py: Sequence class with special methods attributes


In [47]:
class Sequence:
    transcription_table = {'A':'U', 'T':'A', 'C':'G', 'G':'C'}
    comp_table = {'A':'T', 'T':'A', 'C':'G', 'G':'C'}
    def __init__(self, seqstring):
        self.seqstring = seqstring.upper()
    def restriction(self, enz):
        enz_dict = {'EcoRI':'ACTGG', 'EcoRV':'AGTGC'}
        try:
            target = enz_dict[enz]
        except KeyError:
            raise ValueError('No such enzime in out enzime DB')
        return self.seqstring.count(target)
    def __getitem__(self,index):
        return self.seqstring[index]
    def __getslice__(self, low, high):
        return self.seqstring[low:high]
    def __len__(self):
        return len(self.seqstring)
    def __str__(self):
        if len(self.seqstring) >= 28:
            return '{0}...{1}'.format(self.seqstring[:25], 
                                      self.seqstring[-3:])
        else:
            return self.seqstring
    def transcription(self):
        tt = ''
        for x in self.seqstring:
            if x in 'ATCG':
                tt += self.transcription_table[x]
        return tt
    def complement(self):
        tt = ''
        for x in self.seqstring:
            if x in 'ATCG':
                tt += self.comp_table[x]
        return tt

Listing 8.7: zdict.py: Extending dictionary class


In [48]:
class Zdic(dict):
    """ A dictionary-like object that return 0 when a user
    request a non-existent key.
    """

    def __missing__(self,x):
        return 0

In [49]:
a = Zdic()
a['blue'] = 'azul'
a['red']


Out[49]:
0

In [13]:
class TestClass:
    """A class with a "private" method (b)"""
    def a(self):
        pass
    def __b(self):
        # mangled to _TestClass__b
        pass

my_object = TestClass()
print(my_object.a())
my_object.__b()


None
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-07d9210c0261> in <module>()
      9 my_object = TestClass()
     10 print(my_object.a())
---> 11 my_object.__b()

AttributeError: 'TestClass' object has no attribute '__b'

In [14]:
my_object._TestClass__b()